1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2001, 2002, 2003 Broadcom Corporation
5 * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
6 * Copyright (C) 2007 MIPS Technologies, Inc.
7 * written by Ralf Baechle <ralf@linux-mips.org>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/sched.h>
19 #include <linux/vmalloc.h>
21 #include <linux/errno.h>
22 #include <linux/wait.h>
24 #include <asm/sibyte/sb1250.h>
26 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
27 #include <asm/sibyte/bcm1480_regs.h>
28 #include <asm/sibyte/bcm1480_scd.h>
29 #include <asm/sibyte/bcm1480_int.h>
30 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
31 #include <asm/sibyte/sb1250_regs.h>
32 #include <asm/sibyte/sb1250_scd.h>
33 #include <asm/sibyte/sb1250_int.h>
35 #error invalid SiByte UART configuration
38 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
39 #undef K_INT_TRACE_FREEZE
40 #define K_INT_TRACE_FREEZE K_BCM1480_INT_TRACE_FREEZE
42 #define K_INT_PERF_CNT K_BCM1480_INT_PERF_CNT
45 #include <linux/uaccess.h>
47 #define SBPROF_TB_MAJOR 240
49 typedef u64 tb_sample_t
[6*256];
58 wait_queue_head_t tb_sync
;
59 wait_queue_head_t tb_read
;
61 enum open_status open
;
62 tb_sample_t
*sbprof_tbbuf
;
65 volatile int tb_enable
;
66 volatile int tb_armed
;
70 static struct sbprof_tb sbp
;
72 #define MAX_SAMPLE_BYTES (24*1024*1024)
73 #define MAX_TBSAMPLE_BYTES (12*1024*1024)
75 #define MAX_SAMPLES (MAX_SAMPLE_BYTES/sizeof(u_int32_t))
76 #define TB_SAMPLE_SIZE (sizeof(tb_sample_t))
77 #define MAX_TB_SAMPLES (MAX_TBSAMPLE_BYTES/TB_SAMPLE_SIZE)
80 #define SBPROF_ZBSTART _IOW('s', 0, int)
81 #define SBPROF_ZBSTOP _IOW('s', 1, int)
82 #define SBPROF_ZBWAITFULL _IOW('s', 2, int)
85 * Routines for using 40-bit SCD cycle counter
87 * Client responsible for either handling interrupts or making sure
88 * the cycles counter never saturates, e.g., by doing
89 * zclk_timer_init(0) at least every 2^40 - 1 ZCLKs.
93 * Configures SCD counter 0 to count ZCLKs starting from val;
94 * Configures SCD counters1,2,3 to count nothing.
95 * Must not be called while gathering ZBbus profiles.
98 #define zclk_timer_init(val) \
99 __asm__ __volatile__ (".set push;" \
101 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
102 "sd %0, 0x10($8);" /* write val to counter0 */ \
103 "sd %1, 0($8);" /* config counter0 for zclks*/ \
106 /* enable, counter0 */ \
107 : /* inputs */ "r"(val), "r" ((1ULL << 33) | 1ULL) \
108 : /* modifies */ "$8" )
111 /* Reads SCD counter 0 and puts result in value
112 unsigned long long val; */
113 #define zclk_get(val) \
114 __asm__ __volatile__ (".set push;" \
116 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
117 "ld %0, 0x10($8);" /* write val to counter0 */ \
119 : /* outputs */ "=r"(val) \
121 : /* modifies */ "$8" )
123 #define DEVNAME "sb_tbprof"
125 #define TB_FULL (sbp.next_tb_sample == MAX_TB_SAMPLES)
128 * Support for ZBbus sampling using the trace buffer
130 * We use the SCD performance counter interrupt, caused by a Zclk counter
131 * overflow, to trigger the start of tracing.
133 * We set the trace buffer to sample everything and freeze on
136 * We map the interrupt for trace_buffer_freeze to handle it on CPU 0.
140 static u64 tb_period
;
142 static void arm_tb(void)
145 u64 next
= (1ULL << 40) - tb_period
;
146 u64 tb_options
= M_SCD_TRACE_CFG_FREEZE_FULL
;
149 * Generate an SCD_PERFCNT interrupt in TB_PERIOD Zclks to
150 * trigger start of trace. XXX vary sampling period
152 __raw_writeq(0, IOADDR(A_SCD_PERF_CNT_1
));
153 scdperfcnt
= __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG
));
156 * Unfortunately, in Pass 2 we must clear all counters to knock down
157 * a previous interrupt request. This means that bus profiling
158 * requires ALL of the SCD perf counters.
160 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
161 __raw_writeq((scdperfcnt
& ~M_SPC_CFG_SRC1
) |
162 /* keep counters 0,2,3,4,5,6,7 as is */
163 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
164 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG0
));
166 M_SPC_CFG_ENABLE
| /* enable counting */
167 M_SPC_CFG_CLEAR
| /* clear all counters */
168 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
169 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG1
));
171 __raw_writeq((scdperfcnt
& ~M_SPC_CFG_SRC1
) |
172 /* keep counters 0,2,3 as is */
173 M_SPC_CFG_ENABLE
| /* enable counting */
174 M_SPC_CFG_CLEAR
| /* clear all counters */
175 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
176 IOADDR(A_SCD_PERF_CNT_CFG
));
178 __raw_writeq(next
, IOADDR(A_SCD_PERF_CNT_1
));
179 /* Reset the trace buffer */
180 __raw_writeq(M_SCD_TRACE_CFG_RESET
, IOADDR(A_SCD_TRACE_CFG
));
181 #if 0 && defined(M_SCD_TRACE_CFG_FORCECNT)
182 /* XXXKW may want to expose control to the data-collector */
183 tb_options
|= M_SCD_TRACE_CFG_FORCECNT
;
185 __raw_writeq(tb_options
, IOADDR(A_SCD_TRACE_CFG
));
189 static irqreturn_t
sbprof_tb_intr(int irq
, void *dev_id
)
193 pr_debug(DEVNAME
": tb_intr\n");
195 if (sbp
.next_tb_sample
< MAX_TB_SAMPLES
) {
196 /* XXX should use XKPHYS to make writes bypass L2 */
197 u64
*p
= sbp
.sbprof_tbbuf
[sbp
.next_tb_sample
++];
199 __raw_writeq(M_SCD_TRACE_CFG_START_READ
,
200 IOADDR(A_SCD_TRACE_CFG
));
201 __asm__
__volatile__ ("sync" : : : "memory");
202 /* Loop runs backwards because bundles are read out in reverse order */
203 for (i
= 256 * 6; i
> 0; i
-= 6) {
204 /* Subscripts decrease to put bundle in the order */
205 /* t0 lo, t0 hi, t1 lo, t1 hi, t2 lo, t2 hi */
206 p
[i
- 1] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
208 p
[i
- 2] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
210 p
[i
- 3] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
212 p
[i
- 4] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
214 p
[i
- 5] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
216 p
[i
- 6] = __raw_readq(IOADDR(A_SCD_TRACE_READ
));
219 if (!sbp
.tb_enable
) {
220 pr_debug(DEVNAME
": tb_intr shutdown\n");
221 __raw_writeq(M_SCD_TRACE_CFG_RESET
,
222 IOADDR(A_SCD_TRACE_CFG
));
224 wake_up_interruptible(&sbp
.tb_sync
);
226 /* knock down current interrupt and get another one later */
230 /* No more trace buffer samples */
231 pr_debug(DEVNAME
": tb_intr full\n");
232 __raw_writeq(M_SCD_TRACE_CFG_RESET
, IOADDR(A_SCD_TRACE_CFG
));
235 wake_up_interruptible(&sbp
.tb_sync
);
236 wake_up_interruptible(&sbp
.tb_read
);
241 static irqreturn_t
sbprof_pc_intr(int irq
, void *dev_id
)
243 printk(DEVNAME
": unexpected pc_intr");
248 * Requires: Already called zclk_timer_init with a value that won't
249 * saturate 40 bits. No subsequent use of SCD performance counters
253 static int sbprof_zbprof_start(struct file
*filp
)
258 if (xchg(&sbp
.tb_enable
, 1))
261 pr_debug(DEVNAME
": starting\n");
263 sbp
.next_tb_sample
= 0;
266 err
= request_irq(K_INT_TRACE_FREEZE
, sbprof_tb_intr
, 0,
267 DEVNAME
" trace freeze", &sbp
);
271 /* Make sure there isn't a perf-cnt interrupt waiting */
272 scdperfcnt
= __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG
));
273 /* Disable and clear counters, override SRC_1 */
274 __raw_writeq((scdperfcnt
& ~(M_SPC_CFG_SRC1
| M_SPC_CFG_ENABLE
)) |
275 M_SPC_CFG_ENABLE
| M_SPC_CFG_CLEAR
| V_SPC_CFG_SRC1(1),
276 IOADDR(A_SCD_PERF_CNT_CFG
));
279 * We grab this interrupt to prevent others from trying to use
280 * it, even though we don't want to service the interrupts
281 * (they only feed into the trace-on-interrupt mechanism)
283 if (request_irq(K_INT_PERF_CNT
, sbprof_pc_intr
, 0, DEVNAME
" scd perfcnt", &sbp
)) {
284 free_irq(K_INT_TRACE_FREEZE
, &sbp
);
289 * I need the core to mask these, but the interrupt mapper to
290 * pass them through. I am exploiting my knowledge that
291 * cp0_status masks out IP[5]. krw
293 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
294 __raw_writeq(K_BCM1480_INT_MAP_I3
,
295 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_L
) +
296 ((K_BCM1480_INT_PERF_CNT
& 0x3f) << 3)));
298 __raw_writeq(K_INT_MAP_I3
,
299 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE
) +
300 (K_INT_PERF_CNT
<< 3)));
303 /* Initialize address traps */
304 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_0
));
305 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_1
));
306 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_2
));
307 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_3
));
309 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_0
));
310 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_1
));
311 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_2
));
312 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_3
));
314 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_0
));
315 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_1
));
316 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_2
));
317 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_3
));
319 /* Initialize Trace Event 0-7 */
321 __raw_writeq(M_SCD_TREVT_INTERRUPT
, IOADDR(A_SCD_TRACE_EVENT_0
));
322 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_1
));
323 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_2
));
324 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_3
));
325 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_4
));
326 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_5
));
327 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_6
));
328 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_7
));
330 /* Initialize Trace Sequence 0-7 */
331 /* Start on event 0 (interrupt) */
332 __raw_writeq(V_SCD_TRSEQ_FUNC_START
| 0x0fff,
333 IOADDR(A_SCD_TRACE_SEQUENCE_0
));
334 /* dsamp when d used | asamp when a used */
335 __raw_writeq(M_SCD_TRSEQ_ASAMPLE
| M_SCD_TRSEQ_DSAMPLE
|
336 K_SCD_TRSEQ_TRIGGER_ALL
,
337 IOADDR(A_SCD_TRACE_SEQUENCE_1
));
338 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_2
));
339 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_3
));
340 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_4
));
341 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_5
));
342 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_6
));
343 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_7
));
345 /* Now indicate the PERF_CNT interrupt as a trace-relevant interrupt */
346 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
347 __raw_writeq(1ULL << (K_BCM1480_INT_PERF_CNT
& 0x3f),
348 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_TRACE_L
)));
350 __raw_writeq(1ULL << K_INT_PERF_CNT
,
351 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_TRACE
)));
355 pr_debug(DEVNAME
": done starting\n");
360 static int sbprof_zbprof_stop(void)
364 pr_debug(DEVNAME
": stopping\n");
368 * XXXKW there is a window here where the intr handler may run,
369 * see the disable, and do the wake_up before this sleep
372 pr_debug(DEVNAME
": wait for disarm\n");
373 err
= wait_event_interruptible(sbp
.tb_sync
, !sbp
.tb_armed
);
374 pr_debug(DEVNAME
": disarm complete, stat %d\n", err
);
380 free_irq(K_INT_TRACE_FREEZE
, &sbp
);
381 free_irq(K_INT_PERF_CNT
, &sbp
);
384 pr_debug(DEVNAME
": done stopping\n");
389 static int sbprof_tb_open(struct inode
*inode
, struct file
*filp
)
393 minor
= iminor(inode
);
397 if (xchg(&sbp
.open
, SB_OPENING
) != SB_CLOSED
)
400 memset(&sbp
, 0, sizeof(struct sbprof_tb
));
401 sbp
.sbprof_tbbuf
= vzalloc(MAX_TBSAMPLE_BYTES
);
402 if (!sbp
.sbprof_tbbuf
) {
403 sbp
.open
= SB_CLOSED
;
408 init_waitqueue_head(&sbp
.tb_sync
);
409 init_waitqueue_head(&sbp
.tb_read
);
410 mutex_init(&sbp
.lock
);
418 static int sbprof_tb_release(struct inode
*inode
, struct file
*filp
)
422 minor
= iminor(inode
);
423 if (minor
!= 0 || sbp
.open
!= SB_CLOSED
)
426 mutex_lock(&sbp
.lock
);
428 if (sbp
.tb_armed
|| sbp
.tb_enable
)
429 sbprof_zbprof_stop();
431 vfree(sbp
.sbprof_tbbuf
);
432 sbp
.open
= SB_CLOSED
;
435 mutex_unlock(&sbp
.lock
);
440 static ssize_t
sbprof_tb_read(struct file
*filp
, char *buf
,
441 size_t size
, loff_t
*offp
)
443 int cur_sample
, sample_off
, cur_count
, sample_left
;
447 long cur_off
= *offp
;
449 if (!access_ok(buf
, size
))
452 mutex_lock(&sbp
.lock
);
455 cur_sample
= cur_off
/ TB_SAMPLE_SIZE
;
456 sample_off
= cur_off
% TB_SAMPLE_SIZE
;
457 sample_left
= TB_SAMPLE_SIZE
- sample_off
;
459 while (size
&& (cur_sample
< sbp
.next_tb_sample
)) {
462 cur_count
= size
< sample_left
? size
: sample_left
;
463 src
= (char *)(((long)sbp
.sbprof_tbbuf
[cur_sample
])+sample_off
);
464 err
= __copy_to_user(dest
, src
, cur_count
);
466 *offp
= cur_off
+ cur_count
- err
;
467 mutex_unlock(&sbp
.lock
);
470 pr_debug(DEVNAME
": read from sample %d, %d bytes\n",
471 cur_sample
, cur_count
);
473 sample_left
-= cur_count
;
477 sample_left
= TB_SAMPLE_SIZE
;
479 sample_off
+= cur_count
;
481 cur_off
+= cur_count
;
486 mutex_unlock(&sbp
.lock
);
491 static long sbprof_tb_ioctl(struct file
*filp
,
492 unsigned int command
,
499 mutex_lock(&sbp
.lock
);
500 err
= sbprof_zbprof_start(filp
);
501 mutex_unlock(&sbp
.lock
);
505 mutex_lock(&sbp
.lock
);
506 err
= sbprof_zbprof_stop();
507 mutex_unlock(&sbp
.lock
);
510 case SBPROF_ZBWAITFULL
: {
511 err
= wait_event_interruptible(sbp
.tb_read
, TB_FULL
);
515 err
= put_user(TB_FULL
, (int *) arg
);
527 static const struct file_operations sbprof_tb_fops
= {
528 .owner
= THIS_MODULE
,
529 .open
= sbprof_tb_open
,
530 .release
= sbprof_tb_release
,
531 .read
= sbprof_tb_read
,
532 .unlocked_ioctl
= sbprof_tb_ioctl
,
533 .compat_ioctl
= sbprof_tb_ioctl
,
535 .llseek
= default_llseek
,
538 static struct class *tb_class
;
539 static struct device
*tb_dev
;
541 static int __init
sbprof_tb_init(void)
547 if (register_chrdev(SBPROF_TB_MAJOR
, DEVNAME
, &sbprof_tb_fops
)) {
548 printk(KERN_WARNING DEVNAME
": initialization failed (dev %d)\n",
553 tbc
= class_create(THIS_MODULE
, "sb_tracebuffer");
561 dev
= device_create(tbc
, NULL
, MKDEV(SBPROF_TB_MAJOR
, 0), NULL
, "tb");
568 sbp
.open
= SB_CLOSED
;
570 tb_period
= zbbus_mhz
* 10000LL;
571 pr_info(DEVNAME
": initialized - tb_period = %lld\n",
572 (long long) tb_period
);
576 class_destroy(tb_class
);
578 unregister_chrdev(SBPROF_TB_MAJOR
, DEVNAME
);
583 static void __exit
sbprof_tb_cleanup(void)
585 device_destroy(tb_class
, MKDEV(SBPROF_TB_MAJOR
, 0));
586 unregister_chrdev(SBPROF_TB_MAJOR
, DEVNAME
);
587 class_destroy(tb_class
);
590 module_init(sbprof_tb_init
);
591 module_exit(sbprof_tb_cleanup
);
593 MODULE_ALIAS_CHARDEV_MAJOR(SBPROF_TB_MAJOR
);
594 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
595 MODULE_LICENSE("GPL");