2 #include <linux/interrupt.h>
3 #include <asm/octeon/octeon.h>
4 #include <asm/octeon/cvmx-ciu-defs.h>
5 #include <asm/octeon/cvmx.h>
6 #include <linux/debugfs.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/seq_file.h>
13 static bool reset_stats
;
27 static struct latency_info li
;
28 static struct dentry
*dir
;
30 static int show_latency(struct seq_file
*m
, void *v
)
32 u64 cpuclk
, avg
, max
, min
;
33 struct latency_info curr_li
= li
;
35 cpuclk
= octeon_get_clock_rate();
37 max
= (curr_li
.max_latency
* 1000000000) / cpuclk
;
38 min
= (curr_li
.min_latency
* 1000000000) / cpuclk
;
39 avg
= (curr_li
.latency_sum
* 1000000000) / (cpuclk
* curr_li
.interrupt_cnt
);
41 seq_printf(m
, "cnt: %10lld, avg: %7lld ns, max: %7lld ns, min: %7lld ns\n",
42 curr_li
.interrupt_cnt
, avg
, max
, min
);
46 static int oct_ilm_open(struct inode
*inode
, struct file
*file
)
48 return single_open(file
, show_latency
, NULL
);
51 static const struct file_operations oct_ilm_ops
= {
55 .release
= single_release
,
58 static int reset_statistics(void *data
, u64 value
)
64 DEFINE_SIMPLE_ATTRIBUTE(reset_statistics_ops
, NULL
, reset_statistics
, "%llu\n");
66 static int init_debufs(void)
68 struct dentry
*show_dentry
;
69 dir
= debugfs_create_dir("oct_ilm", 0);
71 pr_err("oct_ilm: failed to create debugfs entry oct_ilm\n");
75 show_dentry
= debugfs_create_file("statistics", 0222, dir
, NULL
,
78 pr_err("oct_ilm: failed to create debugfs entry oct_ilm/statistics\n");
82 show_dentry
= debugfs_create_file("reset", 0222, dir
, NULL
,
83 &reset_statistics_ops
);
85 pr_err("oct_ilm: failed to create debugfs entry oct_ilm/reset\n");
93 static void init_latency_info(struct latency_info
*li
, int startup
)
95 /* interval in milli seconds after which the interrupt will
101 /* Calculating by the amounts io clock and cpu clock would
102 * increment in interval amount of ms
104 li
->io_interval
= (octeon_get_io_clock_rate() * interval
) / 1000;
105 li
->cpu_interval
= (octeon_get_clock_rate() * interval
) / 1000;
107 li
->timer_start1
= 0;
108 li
->timer_start2
= 0;
110 li
->min_latency
= (u64
)-1;
112 li
->interrupt_cnt
= 0;
116 static void start_timer(int timer
, u64 interval
)
118 union cvmx_ciu_timx timx
;
123 timx
.s
.len
= interval
;
124 raw_local_irq_save(flags
);
125 li
.timer_start1
= read_c0_cvmcount();
126 cvmx_write_csr(CVMX_CIU_TIMX(timer
), timx
.u64
);
127 /* Read it back to force wait until register is written. */
128 timx
.u64
= cvmx_read_csr(CVMX_CIU_TIMX(timer
));
129 li
.timer_start2
= read_c0_cvmcount();
130 raw_local_irq_restore(flags
);
134 static irqreturn_t
cvm_oct_ciu_timer_interrupt(int cpl
, void *dev_id
)
140 init_latency_info(&li
, 0);
143 last_int_cnt
= read_c0_cvmcount();
144 last_latency
= last_int_cnt
- (li
.timer_start1
+ li
.cpu_interval
);
146 li
.latency_sum
+= last_latency
;
147 if (last_latency
> li
.max_latency
)
148 li
.max_latency
= last_latency
;
149 if (last_latency
< li
.min_latency
)
150 li
.min_latency
= last_latency
;
152 start_timer(TIMER_NUM
, li
.io_interval
);
156 static void disable_timer(int timer
)
158 union cvmx_ciu_timx timx
;
162 cvmx_write_csr(CVMX_CIU_TIMX(timer
), timx
.u64
);
163 /* Read it back to force immediate write of timer register*/
164 timx
.u64
= cvmx_read_csr(CVMX_CIU_TIMX(timer
));
167 static __init
int oct_ilm_module_init(void)
170 int irq
= OCTEON_IRQ_TIMER0
+ TIMER_NUM
;
174 WARN(1, "Could not create debugfs entries");
178 rc
= request_irq(irq
, cvm_oct_ciu_timer_interrupt
, IRQF_NO_THREAD
,
181 WARN(1, "Could not acquire IRQ %d", irq
);
185 init_latency_info(&li
, 1);
186 start_timer(TIMER_NUM
, li
.io_interval
);
190 debugfs_remove_recursive(dir
);
194 static __exit
void oct_ilm_module_exit(void)
196 disable_timer(TIMER_NUM
);
197 debugfs_remove_recursive(dir
);
198 free_irq(OCTEON_IRQ_TIMER0
+ TIMER_NUM
, 0);
201 module_exit(oct_ilm_module_exit
);
202 module_init(oct_ilm_module_init
);
203 MODULE_AUTHOR("Venkat Subbiah, Cavium");
204 MODULE_DESCRIPTION("Measures interrupt latency on Octeon chips.");
205 MODULE_LICENSE("GPL");