]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/mips/sibyte/common/sb_tbprof.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-bionic-kernel.git] / arch / mips / sibyte / common / sb_tbprof.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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.
bb9b813b
RB
15 *
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>
1da177e4
LT
20 */
21
bb9b813b 22#undef DEBUG
1da177e4 23
bb9b813b 24#include <linux/device.h>
1da177e4
LT
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/types.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
1da177e4
LT
30#include <linux/vmalloc.h>
31#include <linux/fs.h>
32#include <linux/errno.h>
db89a48c 33#include <linux/wait.h>
1da177e4
LT
34#include <asm/io.h>
35#include <asm/sibyte/sb1250.h>
d619f38f
MM
36
37#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
38#include <asm/sibyte/bcm1480_regs.h>
39#include <asm/sibyte/bcm1480_scd.h>
40#include <asm/sibyte/bcm1480_int.h>
41#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1da177e4
LT
42#include <asm/sibyte/sb1250_regs.h>
43#include <asm/sibyte/sb1250_scd.h>
44#include <asm/sibyte/sb1250_int.h>
d619f38f
MM
45#else
46#error invalid SiByte UART configuation
47#endif
48
49#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
50#undef K_INT_TRACE_FREEZE
51#define K_INT_TRACE_FREEZE K_BCM1480_INT_TRACE_FREEZE
52#undef K_INT_PERF_CNT
53#define K_INT_PERF_CNT K_BCM1480_INT_PERF_CNT
54#endif
55
bb9b813b
RB
56#include <asm/system.h>
57#include <asm/uaccess.h>
1da177e4 58
bb9b813b
RB
59#define SBPROF_TB_MAJOR 240
60
61typedef u64 tb_sample_t[6*256];
62
63enum open_status {
64 SB_CLOSED,
65 SB_OPENING,
66 SB_OPEN
67};
68
69struct sbprof_tb {
70 wait_queue_head_t tb_sync;
71 wait_queue_head_t tb_read;
72 struct mutex lock;
73 enum open_status open;
74 tb_sample_t *sbprof_tbbuf;
75 int next_tb_sample;
76
77 volatile int tb_enable;
78 volatile int tb_armed;
79
80};
1da177e4
LT
81
82static struct sbprof_tb sbp;
83
bb9b813b
RB
84#define MAX_SAMPLE_BYTES (24*1024*1024)
85#define MAX_TBSAMPLE_BYTES (12*1024*1024)
86
87#define MAX_SAMPLES (MAX_SAMPLE_BYTES/sizeof(u_int32_t))
88#define TB_SAMPLE_SIZE (sizeof(tb_sample_t))
89#define MAX_TB_SAMPLES (MAX_TBSAMPLE_BYTES/TB_SAMPLE_SIZE)
90
91/* ioctls */
92#define SBPROF_ZBSTART _IOW('s', 0, int)
93#define SBPROF_ZBSTOP _IOW('s', 1, int)
94#define SBPROF_ZBWAITFULL _IOW('s', 2, int)
95
96/*
97 * Routines for using 40-bit SCD cycle counter
98 *
99 * Client responsible for either handling interrupts or making sure
100 * the cycles counter never saturates, e.g., by doing
101 * zclk_timer_init(0) at least every 2^40 - 1 ZCLKs.
102 */
103
104/*
105 * Configures SCD counter 0 to count ZCLKs starting from val;
106 * Configures SCD counters1,2,3 to count nothing.
107 * Must not be called while gathering ZBbus profiles.
108 */
109
110#define zclk_timer_init(val) \
111 __asm__ __volatile__ (".set push;" \
112 ".set mips64;" \
113 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
114 "sd %0, 0x10($8);" /* write val to counter0 */ \
115 "sd %1, 0($8);" /* config counter0 for zclks*/ \
116 ".set pop" \
117 : /* no outputs */ \
118 /* enable, counter0 */ \
119 : /* inputs */ "r"(val), "r" ((1ULL << 33) | 1ULL) \
120 : /* modifies */ "$8" )
121
122
123/* Reads SCD counter 0 and puts result in value
124 unsigned long long val; */
125#define zclk_get(val) \
126 __asm__ __volatile__ (".set push;" \
127 ".set mips64;" \
128 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
129 "ld %0, 0x10($8);" /* write val to counter0 */ \
130 ".set pop" \
131 : /* outputs */ "=r"(val) \
132 : /* inputs */ \
133 : /* modifies */ "$8" )
134
d619f38f 135#define DEVNAME "sb_tbprof"
bb9b813b 136
1da177e4
LT
137#define TB_FULL (sbp.next_tb_sample == MAX_TB_SAMPLES)
138
bb9b813b 139/*
1da177e4
LT
140 * Support for ZBbus sampling using the trace buffer
141 *
142 * We use the SCD performance counter interrupt, caused by a Zclk counter
143 * overflow, to trigger the start of tracing.
144 *
145 * We set the trace buffer to sample everything and freeze on
146 * overflow.
147 *
148 * We map the interrupt for trace_buffer_freeze to handle it on CPU 0.
d619f38f 149 *
bb9b813b 150 */
1da177e4 151
bb9b813b 152static u64 tb_period;
1da177e4
LT
153
154static void arm_tb(void)
155{
bb9b813b
RB
156 u64 scdperfcnt;
157 u64 next = (1ULL << 40) - tb_period;
158 u64 tb_options = M_SCD_TRACE_CFG_FREEZE_FULL;
159
160 /*
d619f38f
MM
161 * Generate an SCD_PERFCNT interrupt in TB_PERIOD Zclks to
162 * trigger start of trace. XXX vary sampling period
bb9b813b 163 */
65bda1a9
MR
164 __raw_writeq(0, IOADDR(A_SCD_PERF_CNT_1));
165 scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
bb9b813b
RB
166
167 /*
d619f38f
MM
168 * Unfortunately, in Pass 2 we must clear all counters to knock down
169 * a previous interrupt request. This means that bus profiling
170 * requires ALL of the SCD perf counters.
bb9b813b 171 */
d619f38f
MM
172#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
173 __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
174 /* keep counters 0,2,3,4,5,6,7 as is */
175 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
176 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG0));
177 __raw_writeq(
178 M_SPC_CFG_ENABLE | /* enable counting */
179 M_SPC_CFG_CLEAR | /* clear all counters */
180 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
181 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG1));
182#else
65bda1a9 183 __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
bb9b813b
RB
184 /* keep counters 0,2,3 as is */
185 M_SPC_CFG_ENABLE | /* enable counting */
186 M_SPC_CFG_CLEAR | /* clear all counters */
187 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
65bda1a9 188 IOADDR(A_SCD_PERF_CNT_CFG));
d619f38f 189#endif
65bda1a9 190 __raw_writeq(next, IOADDR(A_SCD_PERF_CNT_1));
1da177e4 191 /* Reset the trace buffer */
65bda1a9 192 __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
1da177e4
LT
193#if 0 && defined(M_SCD_TRACE_CFG_FORCECNT)
194 /* XXXKW may want to expose control to the data-collector */
195 tb_options |= M_SCD_TRACE_CFG_FORCECNT;
196#endif
65bda1a9 197 __raw_writeq(tb_options, IOADDR(A_SCD_TRACE_CFG));
1da177e4
LT
198 sbp.tb_armed = 1;
199}
200
36d98e79 201static irqreturn_t sbprof_tb_intr(int irq, void *dev_id)
1da177e4
LT
202{
203 int i;
bb9b813b
RB
204
205 pr_debug(DEVNAME ": tb_intr\n");
206
1da177e4
LT
207 if (sbp.next_tb_sample < MAX_TB_SAMPLES) {
208 /* XXX should use XKPHYS to make writes bypass L2 */
bb9b813b 209 u64 *p = sbp.sbprof_tbbuf[sbp.next_tb_sample++];
1da177e4 210 /* Read out trace */
65bda1a9
MR
211 __raw_writeq(M_SCD_TRACE_CFG_START_READ,
212 IOADDR(A_SCD_TRACE_CFG));
1da177e4
LT
213 __asm__ __volatile__ ("sync" : : : "memory");
214 /* Loop runs backwards because bundles are read out in reverse order */
215 for (i = 256 * 6; i > 0; i -= 6) {
bb9b813b
RB
216 /* Subscripts decrease to put bundle in the order */
217 /* t0 lo, t0 hi, t1 lo, t1 hi, t2 lo, t2 hi */
65bda1a9 218 p[i - 1] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 219 /* read t2 hi */
65bda1a9 220 p[i - 2] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 221 /* read t2 lo */
65bda1a9 222 p[i - 3] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 223 /* read t1 hi */
65bda1a9 224 p[i - 4] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 225 /* read t1 lo */
65bda1a9 226 p[i - 5] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 227 /* read t0 hi */
65bda1a9 228 p[i - 6] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
d619f38f 229 /* read t0 lo */
1da177e4
LT
230 }
231 if (!sbp.tb_enable) {
bb9b813b 232 pr_debug(DEVNAME ": tb_intr shutdown\n");
65bda1a9
MR
233 __raw_writeq(M_SCD_TRACE_CFG_RESET,
234 IOADDR(A_SCD_TRACE_CFG));
1da177e4 235 sbp.tb_armed = 0;
d619f38f 236 wake_up_interruptible(&sbp.tb_sync);
1da177e4 237 } else {
d619f38f
MM
238 /* knock down current interrupt and get another one later */
239 arm_tb();
1da177e4
LT
240 }
241 } else {
242 /* No more trace buffer samples */
bb9b813b 243 pr_debug(DEVNAME ": tb_intr full\n");
65bda1a9 244 __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
1da177e4 245 sbp.tb_armed = 0;
d619f38f
MM
246 if (!sbp.tb_enable)
247 wake_up_interruptible(&sbp.tb_sync);
248 wake_up_interruptible(&sbp.tb_read);
1da177e4
LT
249 }
250 return IRQ_HANDLED;
251}
252
36d98e79 253static irqreturn_t sbprof_pc_intr(int irq, void *dev_id)
1da177e4
LT
254{
255 printk(DEVNAME ": unexpected pc_intr");
256 return IRQ_NONE;
257}
258
bb9b813b
RB
259/*
260 * Requires: Already called zclk_timer_init with a value that won't
261 * saturate 40 bits. No subsequent use of SCD performance counters
262 * or trace buffer.
263 */
264
265static int sbprof_zbprof_start(struct file *filp)
1da177e4 266{
bb9b813b
RB
267 u64 scdperfcnt;
268 int err;
1da177e4 269
bb9b813b 270 if (xchg(&sbp.tb_enable, 1))
1da177e4
LT
271 return -EBUSY;
272
bb9b813b 273 pr_debug(DEVNAME ": starting\n");
1da177e4 274
1da177e4
LT
275 sbp.next_tb_sample = 0;
276 filp->f_pos = 0;
277
49a89efb
RB
278 err = request_irq(K_INT_TRACE_FREEZE, sbprof_tb_intr, 0,
279 DEVNAME " trace freeze", &sbp);
bb9b813b 280 if (err)
1da177e4 281 return -EBUSY;
bb9b813b 282
1da177e4 283 /* Make sure there isn't a perf-cnt interrupt waiting */
65bda1a9 284 scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
1da177e4 285 /* Disable and clear counters, override SRC_1 */
65bda1a9
MR
286 __raw_writeq((scdperfcnt & ~(M_SPC_CFG_SRC1 | M_SPC_CFG_ENABLE)) |
287 M_SPC_CFG_ENABLE | M_SPC_CFG_CLEAR | V_SPC_CFG_SRC1(1),
288 IOADDR(A_SCD_PERF_CNT_CFG));
1da177e4 289
bb9b813b 290 /*
d619f38f
MM
291 * We grab this interrupt to prevent others from trying to use
292 * it, even though we don't want to service the interrupts
293 * (they only feed into the trace-on-interrupt mechanism)
bb9b813b 294 */
d619f38f
MM
295 if (request_irq(K_INT_PERF_CNT, sbprof_pc_intr, 0, DEVNAME " scd perfcnt", &sbp)) {
296 free_irq(K_INT_TRACE_FREEZE, &sbp);
297 return -EBUSY;
298 }
bb9b813b
RB
299
300 /*
d619f38f
MM
301 * I need the core to mask these, but the interrupt mapper to
302 * pass them through. I am exploiting my knowledge that
303 * cp0_status masks out IP[5]. krw
bb9b813b 304 */
d619f38f
MM
305#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
306 __raw_writeq(K_BCM1480_INT_MAP_I3,
307 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) +
308 ((K_BCM1480_INT_PERF_CNT & 0x3f) << 3)));
309#else
65bda1a9
MR
310 __raw_writeq(K_INT_MAP_I3,
311 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
312 (K_INT_PERF_CNT << 3)));
d619f38f 313#endif
1da177e4
LT
314
315 /* Initialize address traps */
65bda1a9
MR
316 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_0));
317 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_1));
318 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_2));
319 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_3));
1da177e4 320
65bda1a9
MR
321 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_0));
322 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_1));
323 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_2));
324 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_3));
1da177e4 325
65bda1a9
MR
326 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_0));
327 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_1));
328 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_2));
329 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_3));
1da177e4
LT
330
331 /* Initialize Trace Event 0-7 */
d619f38f 332 /* when interrupt */
65bda1a9
MR
333 __raw_writeq(M_SCD_TREVT_INTERRUPT, IOADDR(A_SCD_TRACE_EVENT_0));
334 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_1));
335 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_2));
336 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_3));
337 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_4));
338 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_5));
339 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_6));
340 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_7));
1da177e4
LT
341
342 /* Initialize Trace Sequence 0-7 */
bb9b813b 343 /* Start on event 0 (interrupt) */
65bda1a9
MR
344 __raw_writeq(V_SCD_TRSEQ_FUNC_START | 0x0fff,
345 IOADDR(A_SCD_TRACE_SEQUENCE_0));
bb9b813b 346 /* dsamp when d used | asamp when a used */
65bda1a9
MR
347 __raw_writeq(M_SCD_TRSEQ_ASAMPLE | M_SCD_TRSEQ_DSAMPLE |
348 K_SCD_TRSEQ_TRIGGER_ALL,
349 IOADDR(A_SCD_TRACE_SEQUENCE_1));
350 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_2));
351 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_3));
352 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_4));
353 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_5));
354 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_6));
355 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_7));
1da177e4
LT
356
357 /* Now indicate the PERF_CNT interrupt as a trace-relevant interrupt */
d619f38f
MM
358#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
359 __raw_writeq(1ULL << (K_BCM1480_INT_PERF_CNT & 0x3f),
360 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_TRACE_L)));
361#else
65bda1a9
MR
362 __raw_writeq(1ULL << K_INT_PERF_CNT,
363 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_TRACE)));
d619f38f 364#endif
1da177e4
LT
365 arm_tb();
366
bb9b813b 367 pr_debug(DEVNAME ": done starting\n");
1da177e4
LT
368
369 return 0;
370}
371
bb9b813b 372static int sbprof_zbprof_stop(void)
1da177e4 373{
d619f38f 374 int err = 0;
bb9b813b
RB
375
376 pr_debug(DEVNAME ": stopping\n");
1da177e4
LT
377
378 if (sbp.tb_enable) {
bb9b813b
RB
379 /*
380 * XXXKW there is a window here where the intr handler may run,
381 * see the disable, and do the wake_up before this sleep
382 * happens.
383 */
384 pr_debug(DEVNAME ": wait for disarm\n");
385 err = wait_event_interruptible(sbp.tb_sync, !sbp.tb_armed);
386 pr_debug(DEVNAME ": disarm complete, stat %d\n", err);
387
388 if (err)
389 return err;
390
1da177e4 391 sbp.tb_enable = 0;
1da177e4
LT
392 free_irq(K_INT_TRACE_FREEZE, &sbp);
393 free_irq(K_INT_PERF_CNT, &sbp);
394 }
395
bb9b813b 396 pr_debug(DEVNAME ": done stopping\n");
1da177e4 397
d619f38f 398 return err;
1da177e4
LT
399}
400
401static int sbprof_tb_open(struct inode *inode, struct file *filp)
402{
403 int minor;
404
405 minor = iminor(inode);
36ac829e
RB
406 if (minor != 0)
407 return -ENODEV;
bb9b813b 408
36ac829e
RB
409 if (xchg(&sbp.open, SB_OPENING) != SB_CLOSED)
410 return -EBUSY;
1da177e4
LT
411
412 memset(&sbp, 0, sizeof(struct sbprof_tb));
413 sbp.sbprof_tbbuf = vmalloc(MAX_TBSAMPLE_BYTES);
7558da94 414 if (!sbp.sbprof_tbbuf) {
36ac829e
RB
415 sbp.open = SB_CLOSED;
416 wmb();
417 return -ENOMEM;
7558da94 418 }
36ac829e 419
1da177e4
LT
420 memset(sbp.sbprof_tbbuf, 0, MAX_TBSAMPLE_BYTES);
421 init_waitqueue_head(&sbp.tb_sync);
422 init_waitqueue_head(&sbp.tb_read);
bb9b813b
RB
423 mutex_init(&sbp.lock);
424
425 sbp.open = SB_OPEN;
36ac829e 426 wmb();
1da177e4 427
36ac829e 428 return 0;
1da177e4
LT
429}
430
431static int sbprof_tb_release(struct inode *inode, struct file *filp)
432{
d619f38f 433 int minor;
1da177e4 434
d619f38f 435 minor = iminor(inode);
36ac829e 436 if (minor != 0 || sbp.open != SB_CLOSED)
1da177e4 437 return -ENODEV;
1da177e4 438
bb9b813b
RB
439 mutex_lock(&sbp.lock);
440
441 if (sbp.tb_armed || sbp.tb_enable)
1da177e4 442 sbprof_zbprof_stop();
1da177e4
LT
443
444 vfree(sbp.sbprof_tbbuf);
36ac829e
RB
445 sbp.open = SB_CLOSED;
446 wmb();
1da177e4 447
bb9b813b
RB
448 mutex_unlock(&sbp.lock);
449
1da177e4
LT
450 return 0;
451}
452
453static ssize_t sbprof_tb_read(struct file *filp, char *buf,
454 size_t size, loff_t *offp)
455{
456 int cur_sample, sample_off, cur_count, sample_left;
bb9b813b 457 char *src;
d619f38f
MM
458 int count = 0;
459 char *dest = buf;
460 long cur_off = *offp;
bb9b813b
RB
461
462 if (!access_ok(VERIFY_WRITE, buf, size))
463 return -EFAULT;
464
465 mutex_lock(&sbp.lock);
1da177e4
LT
466
467 count = 0;
468 cur_sample = cur_off / TB_SAMPLE_SIZE;
469 sample_off = cur_off % TB_SAMPLE_SIZE;
470 sample_left = TB_SAMPLE_SIZE - sample_off;
bb9b813b 471
1da177e4 472 while (size && (cur_sample < sbp.next_tb_sample)) {
bb9b813b
RB
473 int err;
474
1da177e4
LT
475 cur_count = size < sample_left ? size : sample_left;
476 src = (char *)(((long)sbp.sbprof_tbbuf[cur_sample])+sample_off);
bb9b813b
RB
477 err = __copy_to_user(dest, src, cur_count);
478 if (err) {
479 *offp = cur_off + cur_count - err;
480 mutex_unlock(&sbp.lock);
481 return err;
482 }
bb9b813b
RB
483 pr_debug(DEVNAME ": read from sample %d, %d bytes\n",
484 cur_sample, cur_count);
1da177e4
LT
485 size -= cur_count;
486 sample_left -= cur_count;
487 if (!sample_left) {
488 cur_sample++;
489 sample_off = 0;
490 sample_left = TB_SAMPLE_SIZE;
491 } else {
492 sample_off += cur_count;
493 }
494 cur_off += cur_count;
495 dest += cur_count;
496 count += cur_count;
497 }
498 *offp = cur_off;
bb9b813b 499 mutex_unlock(&sbp.lock);
1da177e4
LT
500
501 return count;
502}
503
d619f38f
MM
504static long sbprof_tb_ioctl(struct file *filp,
505 unsigned int command,
506 unsigned long arg)
1da177e4 507{
d619f38f 508 int err = 0;
1da177e4
LT
509
510 switch (command) {
511 case SBPROF_ZBSTART:
bb9b813b 512 mutex_lock(&sbp.lock);
d619f38f 513 err = sbprof_zbprof_start(filp);
bb9b813b 514 mutex_unlock(&sbp.lock);
1da177e4 515 break;
bb9b813b 516
1da177e4 517 case SBPROF_ZBSTOP:
bb9b813b 518 mutex_lock(&sbp.lock);
d619f38f 519 err = sbprof_zbprof_stop();
bb9b813b 520 mutex_unlock(&sbp.lock);
1da177e4 521 break;
bb9b813b 522
d619f38f
MM
523 case SBPROF_ZBWAITFULL: {
524 err = wait_event_interruptible(sbp.tb_read, TB_FULL);
525 if (err)
bb9b813b
RB
526 break;
527
d619f38f 528 err = put_user(TB_FULL, (int *) arg);
bb9b813b 529 break;
d619f38f 530 }
bb9b813b 531
1da177e4 532 default:
d619f38f 533 err = -EINVAL;
1da177e4
LT
534 break;
535 }
536
d619f38f 537 return err;
1da177e4
LT
538}
539
5dfe4c96 540static const struct file_operations sbprof_tb_fops = {
1da177e4
LT
541 .owner = THIS_MODULE,
542 .open = sbprof_tb_open,
543 .release = sbprof_tb_release,
544 .read = sbprof_tb_read,
b288f135
RB
545 .unlocked_ioctl = sbprof_tb_ioctl,
546 .compat_ioctl = sbprof_tb_ioctl,
1da177e4
LT
547 .mmap = NULL,
548};
549
bb9b813b
RB
550static struct class *tb_class;
551static struct device *tb_dev;
552
1da177e4
LT
553static int __init sbprof_tb_init(void)
554{
bb9b813b
RB
555 struct device *dev;
556 struct class *tbc;
557 int err;
558
1da177e4
LT
559 if (register_chrdev(SBPROF_TB_MAJOR, DEVNAME, &sbprof_tb_fops)) {
560 printk(KERN_WARNING DEVNAME ": initialization failed (dev %d)\n",
561 SBPROF_TB_MAJOR);
562 return -EIO;
563 }
bb9b813b
RB
564
565 tbc = class_create(THIS_MODULE, "sb_tracebuffer");
566 if (IS_ERR(tbc)) {
567 err = PTR_ERR(tbc);
568 goto out_chrdev;
569 }
570
571 tb_class = tbc;
572
a9b12619 573 dev = device_create(tbc, NULL, MKDEV(SBPROF_TB_MAJOR, 0), NULL, "tb");
bb9b813b
RB
574 if (IS_ERR(dev)) {
575 err = PTR_ERR(dev);
576 goto out_class;
577 }
578 tb_dev = dev;
579
36ac829e
RB
580 sbp.open = SB_CLOSED;
581 wmb();
1da177e4 582 tb_period = zbbus_mhz * 10000LL;
d619f38f
MM
583 pr_info(DEVNAME ": initialized - tb_period = %lld\n",
584 (long long) tb_period);
1da177e4 585 return 0;
bb9b813b
RB
586
587out_class:
588 class_destroy(tb_class);
589out_chrdev:
590 unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
591
592 return err;
1da177e4
LT
593}
594
595static void __exit sbprof_tb_cleanup(void)
596{
bb9b813b 597 device_destroy(tb_class, MKDEV(SBPROF_TB_MAJOR, 0));
1da177e4 598 unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
bb9b813b 599 class_destroy(tb_class);
1da177e4
LT
600}
601
602module_init(sbprof_tb_init);
603module_exit(sbprof_tb_cleanup);
bb9b813b
RB
604
605MODULE_ALIAS_CHARDEV_MAJOR(SBPROF_TB_MAJOR);
606MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
607MODULE_LICENSE("GPL");