oct_ilm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <linux/fs.h>
  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>
  10. #define TIMER_NUM 3
  11. static bool reset_stats;
  12. struct latency_info {
  13. u64 io_interval;
  14. u64 cpu_interval;
  15. u64 timer_start1;
  16. u64 timer_start2;
  17. u64 max_latency;
  18. u64 min_latency;
  19. u64 latency_sum;
  20. u64 average_latency;
  21. u64 interrupt_cnt;
  22. };
  23. static struct latency_info li;
  24. static struct dentry *dir;
  25. static int show_latency(struct seq_file *m, void *v)
  26. {
  27. u64 cpuclk, avg, max, min;
  28. struct latency_info curr_li = li;
  29. cpuclk = octeon_get_clock_rate();
  30. max = (curr_li.max_latency * 1000000000) / cpuclk;
  31. min = (curr_li.min_latency * 1000000000) / cpuclk;
  32. avg = (curr_li.latency_sum * 1000000000) / (cpuclk * curr_li.interrupt_cnt);
  33. seq_printf(m, "cnt: %10lld, avg: %7lld ns, max: %7lld ns, min: %7lld ns\n",
  34. curr_li.interrupt_cnt, avg, max, min);
  35. return 0;
  36. }
  37. static int oct_ilm_open(struct inode *inode, struct file *file)
  38. {
  39. return single_open(file, show_latency, NULL);
  40. }
  41. static const struct file_operations oct_ilm_ops = {
  42. .open = oct_ilm_open,
  43. .read = seq_read,
  44. .llseek = seq_lseek,
  45. .release = single_release,
  46. };
  47. static int reset_statistics(void *data, u64 value)
  48. {
  49. reset_stats = true;
  50. return 0;
  51. }
  52. DEFINE_SIMPLE_ATTRIBUTE(reset_statistics_ops, NULL, reset_statistics, "%llu\n");
  53. static int init_debufs(void)
  54. {
  55. struct dentry *show_dentry;
  56. dir = debugfs_create_dir("oct_ilm", 0);
  57. if (!dir) {
  58. pr_err("oct_ilm: failed to create debugfs entry oct_ilm\n");
  59. return -1;
  60. }
  61. show_dentry = debugfs_create_file("statistics", 0222, dir, NULL,
  62. &oct_ilm_ops);
  63. if (!show_dentry) {
  64. pr_err("oct_ilm: failed to create debugfs entry oct_ilm/statistics\n");
  65. return -1;
  66. }
  67. show_dentry = debugfs_create_file("reset", 0222, dir, NULL,
  68. &reset_statistics_ops);
  69. if (!show_dentry) {
  70. pr_err("oct_ilm: failed to create debugfs entry oct_ilm/reset\n");
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static void init_latency_info(struct latency_info *li, int startup)
  76. {
  77. /* interval in milli seconds after which the interrupt will
  78. * be triggered
  79. */
  80. int interval = 1;
  81. if (startup) {
  82. /* Calculating by the amounts io clock and cpu clock would
  83. * increment in interval amount of ms
  84. */
  85. li->io_interval = (octeon_get_io_clock_rate() * interval) / 1000;
  86. li->cpu_interval = (octeon_get_clock_rate() * interval) / 1000;
  87. }
  88. li->timer_start1 = 0;
  89. li->timer_start2 = 0;
  90. li->max_latency = 0;
  91. li->min_latency = (u64)-1;
  92. li->latency_sum = 0;
  93. li->interrupt_cnt = 0;
  94. }
  95. static void start_timer(int timer, u64 interval)
  96. {
  97. union cvmx_ciu_timx timx;
  98. unsigned long flags;
  99. timx.u64 = 0;
  100. timx.s.one_shot = 1;
  101. timx.s.len = interval;
  102. raw_local_irq_save(flags);
  103. li.timer_start1 = read_c0_cvmcount();
  104. cvmx_write_csr(CVMX_CIU_TIMX(timer), timx.u64);
  105. /* Read it back to force wait until register is written. */
  106. timx.u64 = cvmx_read_csr(CVMX_CIU_TIMX(timer));
  107. li.timer_start2 = read_c0_cvmcount();
  108. raw_local_irq_restore(flags);
  109. }
  110. static irqreturn_t cvm_oct_ciu_timer_interrupt(int cpl, void *dev_id)
  111. {
  112. u64 last_latency;
  113. u64 last_int_cnt;
  114. if (reset_stats) {
  115. init_latency_info(&li, 0);
  116. reset_stats = false;
  117. } else {
  118. last_int_cnt = read_c0_cvmcount();
  119. last_latency = last_int_cnt - (li.timer_start1 + li.cpu_interval);
  120. li.interrupt_cnt++;
  121. li.latency_sum += last_latency;
  122. if (last_latency > li.max_latency)
  123. li.max_latency = last_latency;
  124. if (last_latency < li.min_latency)
  125. li.min_latency = last_latency;
  126. }
  127. start_timer(TIMER_NUM, li.io_interval);
  128. return IRQ_HANDLED;
  129. }
  130. static void disable_timer(int timer)
  131. {
  132. union cvmx_ciu_timx timx;
  133. timx.s.one_shot = 0;
  134. timx.s.len = 0;
  135. cvmx_write_csr(CVMX_CIU_TIMX(timer), timx.u64);
  136. /* Read it back to force immediate write of timer register*/
  137. timx.u64 = cvmx_read_csr(CVMX_CIU_TIMX(timer));
  138. }
  139. static __init int oct_ilm_module_init(void)
  140. {
  141. int rc;
  142. int irq = OCTEON_IRQ_TIMER0 + TIMER_NUM;
  143. rc = init_debufs();
  144. if (rc) {
  145. WARN(1, "Could not create debugfs entries");
  146. return rc;
  147. }
  148. rc = request_irq(irq, cvm_oct_ciu_timer_interrupt, IRQF_NO_THREAD,
  149. "oct_ilm", 0);
  150. if (rc) {
  151. WARN(1, "Could not acquire IRQ %d", irq);
  152. goto err_irq;
  153. }
  154. init_latency_info(&li, 1);
  155. start_timer(TIMER_NUM, li.io_interval);
  156. return 0;
  157. err_irq:
  158. debugfs_remove_recursive(dir);
  159. return rc;
  160. }
  161. static __exit void oct_ilm_module_exit(void)
  162. {
  163. disable_timer(TIMER_NUM);
  164. debugfs_remove_recursive(dir);
  165. free_irq(OCTEON_IRQ_TIMER0 + TIMER_NUM, 0);
  166. }
  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");