op_model_mipsxx.c 7.3 KB

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