1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/interrupt.h>
4 #include <asm/octeon/octeon.h>
5 #include <asm/octeon/cvmx-ciu-defs.h>
6 #include <asm/octeon/cvmx.h>
7 #include <linux/debugfs.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
14 static bool reset_stats
;
28 static struct latency_info li
;
29 static struct dentry
*dir
;
31 static int oct_ilm_show(struct seq_file
*m
, void *v
)
33 u64 cpuclk
, avg
, max
, min
;
34 struct latency_info curr_li
= li
;
36 cpuclk
= octeon_get_clock_rate();
38 max
= (curr_li
.max_latency
* 1000000000) / cpuclk
;
39 min
= (curr_li
.min_latency
* 1000000000) / cpuclk
;
40 avg
= (curr_li
.latency_sum
* 1000000000) / (cpuclk
* curr_li
.interrupt_cnt
);
42 seq_printf(m
, "cnt: %10lld, avg: %7lld ns, max: %7lld ns, min: %7lld ns\n",
43 curr_li
.interrupt_cnt
, avg
, max
, min
);
46 DEFINE_SHOW_ATTRIBUTE(oct_ilm
);
48 static int reset_statistics(void *data
, u64 value
)
54 DEFINE_DEBUGFS_ATTRIBUTE(reset_statistics_ops
, NULL
, reset_statistics
, "%llu\n");
56 static void init_debugfs(void)
58 dir
= debugfs_create_dir("oct_ilm", 0);
59 debugfs_create_file("statistics", 0222, dir
, NULL
, &oct_ilm_fops
);
60 debugfs_create_file("reset", 0222, dir
, NULL
, &reset_statistics_ops
);
63 static void init_latency_info(struct latency_info
*li
, int startup
)
65 /* interval in milli seconds after which the interrupt will
71 /* Calculating by the amounts io clock and cpu clock would
72 * increment in interval amount of ms
74 li
->io_interval
= (octeon_get_io_clock_rate() * interval
) / 1000;
75 li
->cpu_interval
= (octeon_get_clock_rate() * interval
) / 1000;
80 li
->min_latency
= (u64
)-1;
82 li
->interrupt_cnt
= 0;
86 static void start_timer(int timer
, u64 interval
)
88 union cvmx_ciu_timx timx
;
93 timx
.s
.len
= interval
;
94 raw_local_irq_save(flags
);
95 li
.timer_start1
= read_c0_cvmcount();
96 cvmx_write_csr(CVMX_CIU_TIMX(timer
), timx
.u64
);
97 /* Read it back to force wait until register is written. */
98 timx
.u64
= cvmx_read_csr(CVMX_CIU_TIMX(timer
));
99 li
.timer_start2
= read_c0_cvmcount();
100 raw_local_irq_restore(flags
);
104 static irqreturn_t
cvm_oct_ciu_timer_interrupt(int cpl
, void *dev_id
)
110 init_latency_info(&li
, 0);
113 last_int_cnt
= read_c0_cvmcount();
114 last_latency
= last_int_cnt
- (li
.timer_start1
+ li
.cpu_interval
);
116 li
.latency_sum
+= last_latency
;
117 if (last_latency
> li
.max_latency
)
118 li
.max_latency
= last_latency
;
119 if (last_latency
< li
.min_latency
)
120 li
.min_latency
= last_latency
;
122 start_timer(TIMER_NUM
, li
.io_interval
);
126 static void disable_timer(int timer
)
128 union cvmx_ciu_timx timx
;
132 cvmx_write_csr(CVMX_CIU_TIMX(timer
), timx
.u64
);
133 /* Read it back to force immediate write of timer register*/
134 timx
.u64
= cvmx_read_csr(CVMX_CIU_TIMX(timer
));
137 static __init
int oct_ilm_module_init(void)
140 int irq
= OCTEON_IRQ_TIMER0
+ TIMER_NUM
;
144 rc
= request_irq(irq
, cvm_oct_ciu_timer_interrupt
, IRQF_NO_THREAD
,
147 WARN(1, "Could not acquire IRQ %d", irq
);
151 init_latency_info(&li
, 1);
152 start_timer(TIMER_NUM
, li
.io_interval
);
156 debugfs_remove_recursive(dir
);
160 static __exit
void oct_ilm_module_exit(void)
162 disable_timer(TIMER_NUM
);
163 debugfs_remove_recursive(dir
);
164 free_irq(OCTEON_IRQ_TIMER0
+ TIMER_NUM
, 0);
167 module_exit(oct_ilm_module_exit
);
168 module_init(oct_ilm_module_init
);
169 MODULE_AUTHOR("Venkat Subbiah, Cavium");
170 MODULE_DESCRIPTION("Measures interrupt latency on Octeon chips.");
171 MODULE_LICENSE("GPL");