op_model_mipsxx.c 6.8 KB

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