op_model_mipsxx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_VPEID(vpe) ((vpe) << 16)
  20. #define M_PERFCTL_MT_EN(filter) ((filter) << 20)
  21. #define M_TC_EN_ALL M_PERFCTL_MT_EN(0)
  22. #define M_TC_EN_VPE M_PERFCTL_MT_EN(1)
  23. #define M_TC_EN_TC M_PERFCTL_MT_EN(2)
  24. #define M_PERFCTL_TCID(tcid) ((tcid) << 22)
  25. #define M_PERFCTL_WIDE (1UL << 30)
  26. #define M_PERFCTL_MORE (1UL << 31)
  27. #define M_COUNTER_OVERFLOW (1UL << 31)
  28. #ifdef CONFIG_MIPS_MT_SMP
  29. #define WHAT (M_TC_EN_VPE | M_PERFCTL_VPEID(smp_processor_id()))
  30. #else
  31. #define WHAT 0
  32. #endif
  33. #define __define_perf_accessors(r, n, np) \
  34. \
  35. static inline unsigned int r_c0_ ## r ## n(void) \
  36. { \
  37. unsigned int cpu = smp_processor_id(); \
  38. \
  39. switch (cpu) { \
  40. case 0: \
  41. return read_c0_ ## r ## n(); \
  42. case 1: \
  43. return read_c0_ ## r ## np(); \
  44. default: \
  45. BUG(); \
  46. } \
  47. } \
  48. \
  49. static inline void w_c0_ ## r ## n(unsigned int value) \
  50. { \
  51. unsigned int cpu = smp_processor_id(); \
  52. \
  53. switch (cpu) { \
  54. case 0: \
  55. write_c0_ ## r ## n(value); \
  56. return; \
  57. case 1: \
  58. write_c0_ ## r ## np(value); \
  59. return; \
  60. default: \
  61. BUG(); \
  62. } \
  63. } \
  64. __define_perf_accessors(perfcntr, 0, 2)
  65. __define_perf_accessors(perfcntr, 1, 3)
  66. __define_perf_accessors(perfcntr, 2, 2)
  67. __define_perf_accessors(perfcntr, 3, 2)
  68. __define_perf_accessors(perfctrl, 0, 2)
  69. __define_perf_accessors(perfctrl, 1, 3)
  70. __define_perf_accessors(perfctrl, 2, 2)
  71. __define_perf_accessors(perfctrl, 3, 2)
  72. struct op_mips_model op_model_mipsxx_ops;
  73. static struct mipsxx_register_config {
  74. unsigned int control[4];
  75. unsigned int counter[4];
  76. } reg;
  77. /* Compute all of the registers in preparation for enabling profiling. */
  78. static void mipsxx_reg_setup(struct op_counter_config *ctr)
  79. {
  80. unsigned int counters = op_model_mipsxx_ops.num_counters;
  81. int i;
  82. /* Compute the performance counter control word. */
  83. /* For now count kernel and user mode */
  84. for (i = 0; i < counters; i++) {
  85. reg.control[i] = 0;
  86. reg.counter[i] = 0;
  87. if (!ctr[i].enabled)
  88. continue;
  89. reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) |
  90. M_PERFCTL_INTERRUPT_ENABLE;
  91. if (ctr[i].kernel)
  92. reg.control[i] |= M_PERFCTL_KERNEL;
  93. if (ctr[i].user)
  94. reg.control[i] |= M_PERFCTL_USER;
  95. if (ctr[i].exl)
  96. reg.control[i] |= M_PERFCTL_EXL;
  97. reg.counter[i] = 0x80000000 - ctr[i].count;
  98. }
  99. }
  100. /* Program all of the registers in preparation for enabling profiling. */
  101. static void mipsxx_cpu_setup (void *args)
  102. {
  103. unsigned int counters = op_model_mipsxx_ops.num_counters;
  104. switch (counters) {
  105. case 4:
  106. w_c0_perfctrl3(0);
  107. w_c0_perfcntr3(reg.counter[3]);
  108. case 3:
  109. w_c0_perfctrl2(0);
  110. w_c0_perfcntr2(reg.counter[2]);
  111. case 2:
  112. w_c0_perfctrl1(0);
  113. w_c0_perfcntr1(reg.counter[1]);
  114. case 1:
  115. w_c0_perfctrl0(0);
  116. w_c0_perfcntr0(reg.counter[0]);
  117. }
  118. }
  119. /* Start all counters on current CPU */
  120. static void mipsxx_cpu_start(void *args)
  121. {
  122. unsigned int counters = op_model_mipsxx_ops.num_counters;
  123. switch (counters) {
  124. case 4:
  125. w_c0_perfctrl3(WHAT | reg.control[3]);
  126. case 3:
  127. w_c0_perfctrl2(WHAT | reg.control[2]);
  128. case 2:
  129. w_c0_perfctrl1(WHAT | reg.control[1]);
  130. case 1:
  131. w_c0_perfctrl0(WHAT | reg.control[0]);
  132. }
  133. }
  134. /* Stop all counters on current CPU */
  135. static void mipsxx_cpu_stop(void *args)
  136. {
  137. unsigned int counters = op_model_mipsxx_ops.num_counters;
  138. switch (counters) {
  139. case 4:
  140. w_c0_perfctrl3(0);
  141. case 3:
  142. w_c0_perfctrl2(0);
  143. case 2:
  144. w_c0_perfctrl1(0);
  145. case 1:
  146. w_c0_perfctrl0(0);
  147. }
  148. }
  149. static int mipsxx_perfcount_handler(struct pt_regs *regs)
  150. {
  151. unsigned int counters = op_model_mipsxx_ops.num_counters;
  152. unsigned int control;
  153. unsigned int counter;
  154. int handled = 0;
  155. switch (counters) {
  156. #define HANDLE_COUNTER(n) \
  157. case n + 1: \
  158. control = r_c0_perfctrl ## n(); \
  159. counter = r_c0_perfcntr ## n(); \
  160. if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
  161. (counter & M_COUNTER_OVERFLOW)) { \
  162. oprofile_add_sample(regs, n); \
  163. w_c0_perfcntr ## n(reg.counter[n]); \
  164. handled = 1; \
  165. }
  166. HANDLE_COUNTER(3)
  167. HANDLE_COUNTER(2)
  168. HANDLE_COUNTER(1)
  169. HANDLE_COUNTER(0)
  170. }
  171. return handled;
  172. }
  173. #define M_CONFIG1_PC (1 << 4)
  174. static inline int __n_counters(void)
  175. {
  176. if (!(read_c0_config1() & M_CONFIG1_PC))
  177. return 0;
  178. if (!(r_c0_perfctrl0() & M_PERFCTL_MORE))
  179. return 1;
  180. if (!(r_c0_perfctrl1() & M_PERFCTL_MORE))
  181. return 2;
  182. if (!(r_c0_perfctrl2() & M_PERFCTL_MORE))
  183. return 3;
  184. return 4;
  185. }
  186. static inline int n_counters(void)
  187. {
  188. int counters = __n_counters();
  189. #ifndef CONFIG_SMP
  190. if (current_cpu_data.cputype == CPU_34K)
  191. return counters >> 1;
  192. #endif
  193. return counters;
  194. }
  195. static inline void reset_counters(int counters)
  196. {
  197. switch (counters) {
  198. case 4:
  199. w_c0_perfctrl3(0);
  200. w_c0_perfcntr3(0);
  201. case 3:
  202. w_c0_perfctrl2(0);
  203. w_c0_perfcntr2(0);
  204. case 2:
  205. w_c0_perfctrl1(0);
  206. w_c0_perfcntr1(0);
  207. case 1:
  208. w_c0_perfctrl0(0);
  209. w_c0_perfcntr0(0);
  210. }
  211. }
  212. static int __init mipsxx_init(void)
  213. {
  214. int counters;
  215. counters = n_counters();
  216. if (counters == 0) {
  217. printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
  218. return -ENODEV;
  219. }
  220. reset_counters(counters);
  221. op_model_mipsxx_ops.num_counters = counters;
  222. switch (current_cpu_data.cputype) {
  223. case CPU_20KC:
  224. op_model_mipsxx_ops.cpu_type = "mips/20K";
  225. break;
  226. case CPU_24K:
  227. op_model_mipsxx_ops.cpu_type = "mips/24K";
  228. break;
  229. case CPU_25KF:
  230. op_model_mipsxx_ops.cpu_type = "mips/25K";
  231. break;
  232. case CPU_34K:
  233. op_model_mipsxx_ops.cpu_type = "mips/34K";
  234. break;
  235. case CPU_74K:
  236. op_model_mipsxx_ops.cpu_type = "mips/74K";
  237. break;
  238. case CPU_5KC:
  239. op_model_mipsxx_ops.cpu_type = "mips/5K";
  240. break;
  241. case CPU_SB1:
  242. case CPU_SB1A:
  243. op_model_mipsxx_ops.cpu_type = "mips/sb1";
  244. break;
  245. default:
  246. printk(KERN_ERR "Profiling unsupported for this CPU\n");
  247. return -ENODEV;
  248. }
  249. perf_irq = mipsxx_perfcount_handler;
  250. return 0;
  251. }
  252. static void mipsxx_exit(void)
  253. {
  254. reset_counters(op_model_mipsxx_ops.num_counters);
  255. perf_irq = null_perf_irq;
  256. }
  257. struct op_mips_model op_model_mipsxx_ops = {
  258. .reg_setup = mipsxx_reg_setup,
  259. .cpu_setup = mipsxx_cpu_setup,
  260. .init = mipsxx_init,
  261. .exit = mipsxx_exit,
  262. .cpu_start = mipsxx_cpu_start,
  263. .cpu_stop = mipsxx_cpu_stop,
  264. };