op_model_mipsxx.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004, 2005 by Ralf Baechle
  7. * Copyright (C) 2005 by MIPS Technologies, Inc.
  8. */
  9. #include <linux/oprofile.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/smp.h>
  12. #include "op_impl.h"
  13. #define M_PERFCTL_EXL (1UL << 0)
  14. #define M_PERFCTL_KERNEL (1UL << 1)
  15. #define M_PERFCTL_SUPERVISOR (1UL << 2)
  16. #define M_PERFCTL_USER (1UL << 3)
  17. #define M_PERFCTL_INTERRUPT_ENABLE (1UL << 4)
  18. #define M_PERFCTL_EVENT(event) ((event) << 5)
  19. #define M_PERFCTL_WIDE (1UL << 30)
  20. #define M_PERFCTL_MORE (1UL << 31)
  21. #define M_COUNTER_OVERFLOW (1UL << 31)
  22. struct op_mips_model op_model_mipsxx;
  23. static struct mipsxx_register_config {
  24. unsigned int control[4];
  25. unsigned int counter[4];
  26. } reg;
  27. /* Compute all of the registers in preparation for enabling profiling. */
  28. static void mipsxx_reg_setup(struct op_counter_config *ctr)
  29. {
  30. unsigned int counters = op_model_mipsxx.num_counters;
  31. int i;
  32. /* Compute the performance counter control word. */
  33. /* For now count kernel and user mode */
  34. for (i = 0; i < counters; i++) {
  35. reg.control[i] = 0;
  36. reg.counter[i] = 0;
  37. if (!ctr[i].enabled)
  38. continue;
  39. reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) |
  40. M_PERFCTL_INTERRUPT_ENABLE;
  41. if (ctr[i].kernel)
  42. reg.control[i] |= M_PERFCTL_KERNEL;
  43. if (ctr[i].user)
  44. reg.control[i] |= M_PERFCTL_USER;
  45. if (ctr[i].exl)
  46. reg.control[i] |= M_PERFCTL_EXL;
  47. reg.counter[i] = 0x80000000 - ctr[i].count;
  48. }
  49. }
  50. /* Program all of the registers in preparation for enabling profiling. */
  51. static void mipsxx_cpu_setup (void *args)
  52. {
  53. unsigned int counters = op_model_mipsxx.num_counters;
  54. switch (counters) {
  55. case 4:
  56. write_c0_perfctrl3(0);
  57. write_c0_perfcntr3(reg.counter[3]);
  58. case 3:
  59. write_c0_perfctrl2(0);
  60. write_c0_perfcntr2(reg.counter[2]);
  61. case 2:
  62. write_c0_perfctrl1(0);
  63. write_c0_perfcntr1(reg.counter[1]);
  64. case 1:
  65. write_c0_perfctrl0(0);
  66. write_c0_perfcntr0(reg.counter[0]);
  67. }
  68. }
  69. /* Start all counters on current CPU */
  70. static void mipsxx_cpu_start(void *args)
  71. {
  72. unsigned int counters = op_model_mipsxx.num_counters;
  73. switch (counters) {
  74. case 4:
  75. write_c0_perfctrl3(reg.control[3]);
  76. case 3:
  77. write_c0_perfctrl2(reg.control[2]);
  78. case 2:
  79. write_c0_perfctrl1(reg.control[1]);
  80. case 1:
  81. write_c0_perfctrl0(reg.control[0]);
  82. }
  83. }
  84. /* Stop all counters on current CPU */
  85. static void mipsxx_cpu_stop(void *args)
  86. {
  87. unsigned int counters = op_model_mipsxx.num_counters;
  88. switch (counters) {
  89. case 4:
  90. write_c0_perfctrl3(0);
  91. case 3:
  92. write_c0_perfctrl2(0);
  93. case 2:
  94. write_c0_perfctrl1(0);
  95. case 1:
  96. write_c0_perfctrl0(0);
  97. }
  98. }
  99. static int mipsxx_perfcount_handler(struct pt_regs *regs)
  100. {
  101. unsigned int counters = op_model_mipsxx.num_counters;
  102. unsigned int control;
  103. unsigned int counter;
  104. int handled = 0;
  105. switch (counters) {
  106. #define HANDLE_COUNTER(n) \
  107. case n + 1: \
  108. control = read_c0_perfctrl ## n(); \
  109. counter = read_c0_perfcntr ## n(); \
  110. if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
  111. (counter & M_COUNTER_OVERFLOW)) { \
  112. oprofile_add_sample(regs, n); \
  113. write_c0_perfcntr ## n(reg.counter[n]); \
  114. handled = 1; \
  115. }
  116. HANDLE_COUNTER(3)
  117. HANDLE_COUNTER(2)
  118. HANDLE_COUNTER(1)
  119. HANDLE_COUNTER(0)
  120. }
  121. return handled;
  122. }
  123. #define M_CONFIG1_PC (1 << 4)
  124. static inline int n_counters(void)
  125. {
  126. if (!(read_c0_config1() & M_CONFIG1_PC))
  127. return 0;
  128. if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
  129. return 1;
  130. if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
  131. return 2;
  132. if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
  133. return 3;
  134. return 4;
  135. }
  136. static inline void reset_counters(int counters)
  137. {
  138. switch (counters) {
  139. case 4:
  140. write_c0_perfctrl3(0);
  141. write_c0_perfcntr3(0);
  142. case 3:
  143. write_c0_perfctrl2(0);
  144. write_c0_perfcntr2(0);
  145. case 2:
  146. write_c0_perfctrl1(0);
  147. write_c0_perfcntr1(0);
  148. case 1:
  149. write_c0_perfctrl0(0);
  150. write_c0_perfcntr0(0);
  151. }
  152. }
  153. static int __init mipsxx_init(void)
  154. {
  155. int counters;
  156. counters = n_counters();
  157. if (counters == 0) {
  158. printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
  159. return -ENODEV;
  160. }
  161. reset_counters(counters);
  162. op_model_mipsxx.num_counters = counters;
  163. switch (current_cpu_data.cputype) {
  164. case CPU_20KC:
  165. op_model_mipsxx.cpu_type = "mips/20K";
  166. break;
  167. case CPU_24K:
  168. op_model_mipsxx.cpu_type = "mips/24K";
  169. break;
  170. case CPU_25KF:
  171. op_model_mipsxx.cpu_type = "mips/25K";
  172. break;
  173. case CPU_5KC:
  174. op_model_mipsxx.cpu_type = "mips/5K";
  175. break;
  176. default:
  177. printk(KERN_ERR "Profiling unsupported for this CPU\n");
  178. return -ENODEV;
  179. }
  180. perf_irq = mipsxx_perfcount_handler;
  181. return 0;
  182. }
  183. static void mipsxx_exit(void)
  184. {
  185. reset_counters(op_model_mipsxx.num_counters);
  186. perf_irq = null_perf_irq;
  187. }
  188. struct op_mips_model op_model_mipsxx = {
  189. .reg_setup = mipsxx_reg_setup,
  190. .cpu_setup = mipsxx_cpu_setup,
  191. .init = mipsxx_init,
  192. .exit = mipsxx_exit,
  193. .cpu_start = mipsxx_cpu_start,
  194. .cpu_stop = mipsxx_cpu_stop,
  195. };