op_model_mipsxx.c 6.8 KB

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